package cn.boge.org.service.impl;

import cn.boge.best.service.impl.BestServiceImpl;
import cn.boge.org.domain.Department;
import cn.boge.org.mapper.DepartmentMapper;
import cn.boge.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BestServiceImpl<Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        //包含主键的department对象
        departmentMapper.add(department);
        upt(department);
    }

    @Override
    @Transactional
    public void upt(Department department) {
        //1.判断 上级parent 是否为空
        if(department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirPath
            Department parent = departmentMapper.findById(department.getParent().getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());

        }
        departmentMapper.upt(department);
    }


    @Override
    public List<Department> loadDeptTree() {
        return departmentMapper.loadDeptTree();
    }
}
